<template>
    <view>
        <form @reset="formReset" @submit="formSubmit">
            <view class="container">
                <view class="head">
                    <view @tap="chooseLogo" class="left">
                        <image class="img" mode="aspectFill" :src="logo"></image>
                    </view>
                    <view class="right">
                        <view class="weui-cell weui-cell_input">
                            <view class="weui-cell__hd">
                                <view class="weui-label">商家名称</view>
                            </view>
                            <view class="weui-cell__bd">
                                <input class="weui-input" :disabled="true" name="sjmc" placeholder="请输入商家名称" :value="sjmc" />
                            </view>
                        </view>
                        <view class="weui-cell weui-cell_input">
                            <view class="weui-cell__hd">
                                <view class="weui-label">商家电话</view>
                            </view>
                            <view class="weui-cell__bd">
                                <input class="weui-input" name="sjdh" placeholder="请输入商家电话" :value="sjdh" />
                            </view>
                        </view>
                    </view>
                </view>
                <view class="bottom">
                    <text>门店位置</text>
                    <textarea name="mdwz" placeholder="请定位商家位置信息，定位后可再进行编辑" :value="weizhi"></textarea>
                    <view @tap="dw" class="dw">
                        <image src="https://ymqycc.oss-cn-chengdu.aliyuncs.com/ymqzho2o/static/icon/address.png"></image>
                        <view>定位</view>
                    </view>
                </view>
                <view class="main">
                    <view class="bd">
                        <view class="left">地址坐标：</view>
                        <view class="right">
                            <view class="weui-cell weui-cell_input">
                                <view class="weui-cell__bd">
                                    <input class="weui-input" :disabled="true" name="jwd" :value="jwd" />
                                </view>
                            </view>
                        </view>
                    </view>
                    <view class="bd">
                        <view class="left">人均价：</view>
                        <view class="right">
                            <view class="weui-cell weui-cell_input">
                                <view class="weui-cell__bd">
                                    <input class="weui-input" name="rjj" placeholder="请输入人均价" type="number" :value="rjj" />
                                </view>
                            </view>
                        </view>
                    </view>
                    <view class="bd">
                        <view class="left">起送价：</view>
                        <view class="right">
                            <view class="weui-cell weui-cell_input">
                                <view class="weui-cell__bd">
                                    <input class="weui-input" name="qsj" placeholder="请输入起送价" type="number" :value="qsj" />
                                </view>
                            </view>
                        </view>
                    </view>
                    <view class="bd">
                        <view class="left">新用户优惠：</view>
                        <view class="right">
                            <view class="weui-cell weui-cell_input">
                                <view class="weui-cell__bd">
                                    <text style="color: #999; font-size: 22rpx; margin-left: 20rpx"></text>
                                </view>
                                <view class="weui-cell__ft">
                                    <switch :checked="xyh_open" name="xyh"></switch>
                                </view>
                            </view>
                        </view>
                    </view>
                    <view class="bd">
                        <view class="left">新用户立减金额：</view>
                        <view class="right">
                            <view class="weui-cell weui-cell_input" style="border: none">
                                <view class="weui-cell__bd">
                                    <input class="weui-input" name="xyhje" placeholder="请输入新用户立减金额" type="number" :value="xyhje" />
                                </view>
                            </view>
                        </view>
                    </view>
                </view>
                <view class="gg">
                    <view class="left">门店公告：</view>
                    <view class="right">
                        <view class="weui-cell">
                            <view class="weui-cell__bd">
                                <textarea @input="gongg" class="weui-textarea" maxlength="100" name="mdgg" placeholder="请输入文本" style="height: 4.5em" :value="mdgg"></textarea>
                                <view class="weui-textarea-counter">
                                    <text style="color: #f44444">{{ zsnum }}</text>
                                    /100个字
                                </view>
                            </view>
                        </view>
                    </view>
                </view>
                <view class="mdtp">
                    <view class="left">商家环境：</view>
                    <view class="right">
                        <view class="tpjy"></view>
                    </view>
                </view>
                <view class="mdtp1">
                    <view class="weui-uploader__bd">
                        <view class="weui-uploader__files" id="uploaderFiles">
                            <view class="weui-uploader__file" v-for="(item, index) in images" :key="index">
                                <view @tap="deleteFun" class="x" :data-index="index">
                                    <image src="https://yunkeososyunchuc.bc8t.cn/ymq_canyino2o/static/seller/x.png"></image>
                                </view>

                                <image @tap="previewImage" class="weui-uploader__img" mode="aspectFill" :src="url + item"></image>
                            </view>
                            <view class="weui-uploader__file" v-for="(item, index) in images1" :key="index">
                                <view @tap="delete1" class="x" :data-index="index">
                                    <image src="https://yunkeososyunchuc.bc8t.cn/ymq_canyino2o/static/seller/x.png"></image>
                                </view>

                                <image @tap="previewImage" class="weui-uploader__img" mode="aspectFill" :src="item"></image>
                            </view>
                        </view>
                        <view class="weui-uploader__input-box" v-if="images.length + images1.length < 9">
                            <view @tap="chooseImage1" class="weui-uploader__input"></view>
                        </view>
                    </view>
                    <view class="tpsl">{{ images.length + images1.length }}/9</view>
                </view>
                <view class="button">
                    <button class="weui-btn" formType="submit" :style="'background-color:' + color" type="primary">保存提交</button>
                </view>
            </view>
        </form>
        <view class="zhezhao" v-if="!fwxy"></view>
        <view class="kcrzxy" v-if="!fwxy">
            <view class="kcrzxyhd">平台合作协议</view>
            <scroll-view scrollY class="kcrzxybd" style="height: 600rpx">
                <rich-text :nodes="ptxx.rz_agreement"></rich-text>
            </scroll-view>
            <view @tap="queren" class="queren">确定</view>
        </view>
    </view>
</template>

<script>
import cardlist from '@/ymq_canyino2o/dist/cardlist/index';
import gettel from '@/ymq_canyino2o/dist/gettel/index';
var imgArray;
var lbimgArray;
var app = getApp();
var util = require('../../../utils/util.js');
var siteinfo = require('../../../../siteinfo.js');
var imgArray1 = [];
var lbimgArray1 = [];
var imglogo = '';
export default {
    components: {
        cardlist,
        gettel
    },
    data() {
        return {
            mdid: '',
            issq: false,
            isbj: true,
            isyz: true,
            isbd: false,
            url1: '',
            url2: '',
            VerifyCode: '验证',
            bdsjhtext: '验证微信手机号',
            lxr: '',
            sjh: '',
            yzm: '',
            jwd: '',
            mdmc: '',
            mdgg: '',
            zsnum: 0,
            hy: [],
            checkbox: [],
            hyIndex: 0,
            timestart: '06:00',
            timeend: '22:00',
            weizhi: '',

            checkboxItems: [
                {
                    name: 'WIFI',
                    value: 'WIFI'
                },
                {
                    name: '停车位',
                    value: '停车位'
                },
                {
                    name: '支付宝支付',
                    value: '支付宝支付'
                },
                {
                    name: '微信支付',
                    value: '微信支付'
                }
            ],

            logo: '/static/ymq_canyino2o/pages/img/logo.png',
            images: [],
            images1: [],
            lbimages: [],
            lbimages1: [],
            uploadedImages: [],
            fwxy: true,
            url: '',
            sjmc: '',
            sjdh: '',
            rjj: '',
            qsj: '',
            xyhje: '',
            xyh_open: '',
            hyid: '',
            color: '',

            ptxx: {
                rz_agreement: ''
            }
        };
    },
    onLoad: function (e) {
        imgArray = [];
        imgArray1 = [];
        lbimgArray = [];
        lbimgArray1 = [];
        var a = uni.getStorageSync('users').id;
        var t = uni.getStorageSync('sjdsjid');
        var that = this;
        console.log(getApp().globalData.getuniacid, a, t);
        app.globalData.setNavigationBarColor(this);
        app.globalData.util.request({
            url: 'entry/wxapp/Url',
            cachetime: '0',
            success: function (e) {
                console.log(e);
                that.setData({
                    url: e.data
                });
            }
        });
        app.globalData.util.request({
            url: 'entry/wxapp/StoreInfo',
            cachetime: '0',
            data: {
                store_id: t
            },
            success: function (e) {
                console.log(e);
                var a = e.data.store;
                imglogo = a.logo;
                imgArray = a.environment;
                lbimgArray = a.yyzz;
                that.setData({
                    logo: a.logo,
                    sjmc: a.name,
                    sjdh: a.tel,
                    weizhi: a.address,
                    jwd: a.coordinates,
                    rjj: a.capita,
                    qsj: a.start_at,
                    xyhje: e.data.storeset.xyh_money,
                    mdgg: a.announcement,
                    zsnum: parseInt(a.announcement.length),
                    images: a.environment,
                    lbimages: a.yyzz,
                    xyh_open: 1 == e.data.storeset.xyh_open
                });
                console.log(
                    'imgArray',
                    imgArray,
                    'imgArray1',
                    imgArray1,
                    'images',
                    that.images,
                    'images1',
                    that.images1,
                    'lbimgArray',
                    lbimgArray,
                    'lbimgArray1',
                    lbimgArray1,
                    'lbimages',
                    that.lbimages,
                    'lbimages1',
                    that.lbimages1
                );
            }
        });
    },
    onReady: function () {},
    onShow: function () {},
    onHide: function () {},
    onUnload: function () {},
    onPullDownRefresh: function () {},
    onReachBottom: function () {},
    methods: {
        lookck: function () {
            this.setData({
                fwxy: false
            });
        },

        queren: function () {
            this.setData({
                fwxy: true
            });
        },

        hqsjh: function (e) {
            console.log(e.detail.value);
            this.setData({
                sjh: e.detail.value
            });
            if ('' != e.detail.value) {
                this.setData({
                    isyz: false
                });
            } else {
                this.setData({
                    isyz: true
                });
            }
        },

        setVerify: function () {
            var e = util.getRandomNum();
            this.setData({
                yzm: e
            });
            var a = this.sjh;
            console.log(a);
            console.log(e);
            var t = 60;
            var that = this;
            if (!/^0?(13[0-9]|15[012356789]|17[013678]|18[0-9]|14[57])[0-9]{8}$/.test(a) || 11 != a.length) {
                uni.showToast({
                    title: '手机号错误',
                    duration: 1000
                });
                return false;
            }
            var i = setInterval(function () {
                if (0 < --t) {
                    that.setData({
                        VerifyCode: t + ' 秒',
                        isyz: true
                    });
                } else {
                    that.setData({
                        VerifyCode: '验证',
                        isyz: false
                    });
                    clearInterval(i);
                }
            }, 1000);
            app.globalData.util.request({
                url: 'entry/wxapp/sms2',
                cachetime: '0',
                headers: {
                    'Content-Type': 'application/json'
                },
                data: {
                    tel: a,
                    code: e
                },
                success: function (e) {
                    console.log('111111111');
                    console.log(e);
                    if ('操作成功' == e.data.reason) {
                        uni.showToast({
                            title: '发送成功',
                            icon: 'success',
                            duration: 1000
                        });
                    }
                },
                fail: function (e) {
                    console.log('error res=');
                    console.log(e.data);
                }
            });
        },

        dw: function () {
            var that = this;
            uni.chooseLocation({
                success: function (e) {
                    console.log(e);
                    that.setData({
                        weizhi: e.address,
                        jwd: e.latitude + ',' + e.longitude
                    });
                }
            });
        },

        bindTimeChange: function (e) {
            console.log(e.detail.value);
            this.setData({
                timestart: e.detail.value
            });
        },

        bindTimeChange1: function (e) {
            console.log(e.detail.value);
            this.setData({
                timeend: e.detail.value
            });
        },

        bindTypeChange: function (e) {
            console.log('picker type 发生选择改变，携带值为', e.detail.value, this.hy[e.detail.value].id);
            this.setData({
                hyIndex: e.detail.value,
                hyid: this.hy[e.detail.value].id
            });
        },

        checkboxChange: function (e) {
            console.log('checkbox发生change事件，携带value值为：', e.detail.value);
            this.setData({
                checkbox: e.detail.value
            });
            console.log(util.in_array('WIFI', e.detail.value));
            for (var a = this.checkboxItems, t = e.detail.value, o = 0, i = a.length; o < i; ++o) {
                a[o].checked = false;
                for (var l = 0, s = t.length; l < s; ++l) {
                    if (a[o].value == t[l]) {
                        a[o].checked = true;
                        break;
                    }
                }
            }
            this.setData({
                checkboxItems: a
            });
        },

        chooseLogo: function () {
            var that = this;
            uni.chooseImage({
                count: 1,
                sizeType: ['compressed'],
                sourceType: ['album', 'camera'],
                success: function (e) {
                    uni.showToast({
                        icon: 'loading',
                        title: '正在上传'
                    });
                    var a = e.tempFilePaths;
                    console.log(a);
                    uni.uploadFile({
                        url: siteinfo.siteroot + '?i=' + siteinfo.uniacid + '&c=entry&a=wxapp&do=upload&m=ymq_canyino2o',
                        filePath: e.tempFilePaths[0],
                        name: 'upfile',
                        success: function (e) {
                            console.log(e);
                            imglogo = e.data;
                            if ('' != e.data) {
                                that.setData({
                                    logo: a[0]
                                });
                            } else {
                                uni.showModal({
                                    title: '提示',
                                    content: '上传失败',
                                    showCancel: false
                                });
                            }
                        },
                        fail: function (e) {
                            console.log(e);
                            uni.showModal({
                                title: '提示',
                                content: '上传失败',
                                showCancel: false
                            });
                        },
                        complete: function () {
                            uni.hideToast();
                        }
                    });
                }
            });
        },

        chooseImage1: function () {
            var that = this;
            var i = this.images1;
            imgArray1 = [];
            uni.chooseImage({
                count: 9 - i.length - that.images.length,
                sizeType: ['compressed'],
                sourceType: ['album', 'camera'],
                success: function (e) {
                    var a = e.tempFilePaths;
                    console.log(a);
                    var t = e.tempFilePaths;
                    i = i.concat(t);
                    console.log(i);
                    that.setData({
                        images1: i
                    });
                }
            });
        },

        lbchooseImage1: function () {
            var that = this;
            var i = this.lbimages1;
            lbimgArray1 = [];
            uni.chooseImage({
                count: 3 - i.length - that.lbimages.length,
                sizeType: ['compressed'],
                sourceType: ['album', 'camera'],
                success: function (e) {
                    uni.showToast({
                        icon: 'loading',
                        title: '正在上传'
                    });
                    var a = e.tempFilePaths;
                    console.log(a);
                    var t = e.tempFilePaths;
                    i = i.concat(t);
                    console.log(i);
                    that.lbuploadimg1({
                        url: getApp().globalData.imglink + 'app/index.php?i=' + getApp().globalData.getuniacid + '&c=entry&a=wxapp&do=upload&m=zh_zbkq',
                        path: i
                    });
                }
            });
        },

        previewImage: function () {
            uni.previewImage({
                urls: this.images
            });
        },

        uploadimg1: function (e) {
            var that = this;
            var t = e.i ? e.i : 0;
            var o = e.success ? e.success : 0;
            var i = e.fail ? e.fail : 0;
            uni.uploadFile({
                url: e.url,
                filePath: e.path[t],
                name: 'upfile',
                formData: null,
                success: function (e) {
                    if ('' != e.data) {
                        console.log(e);
                        o++;
                        imgArray1.push(e.data);
                        console.log(t);
                        console.log('编辑信息时候提交的图片数组', imgArray1);
                    } else {
                        uni.showToast({
                            icon: 'loading',
                            title: '请重试'
                        });
                    }
                },
                fail: function (e) {
                    i++;
                    console.log('fail:' + t + 'fail:' + i);
                },
                complete: function () {
                    console.log(t);
                    if (++t == e.path.length) {
                        that.setData({
                            images1: e.path
                        });
                        uni.hideToast();
                        console.log('执行完毕');
                        console.log('成功：' + o + ' 失败：' + i);
                    } else {
                        console.log(t);
                        e.i = t;
                        e.success = o;
                        e.fail = i;
                        that.uploadimg1(e);
                    }
                }
            });
        },

        lbuploadimg1: function (e) {
            var that = this;
            var t = e.i ? e.i : 0;
            var o = e.success ? e.success : 0;
            var i = e.fail ? e.fail : 0;
            uni.uploadFile({
                url: e.url,
                filePath: e.path[t],
                name: 'upfile',
                formData: null,
                success: function (e) {
                    if ('' != e.data) {
                        console.log(e);
                        o++;
                        lbimgArray1.push(e.data);
                        console.log(t);
                        console.log('编辑信息时候提交的轮播图片数组', lbimgArray1);
                    } else {
                        uni.showToast({
                            icon: 'loading',
                            title: '请重试'
                        });
                    }
                },
                fail: function (e) {
                    i++;
                    console.log('fail:' + t + 'fail:' + i);
                },
                complete: function () {
                    console.log(t);
                    if (++t == e.path.length) {
                        that.setData({
                            lbimages1: e.path
                        });
                        uni.hideToast();
                        console.log('执行完毕');
                        console.log('成功：' + o + ' 失败：' + i);
                    } else {
                        console.log(t);
                        e.i = t;
                        e.success = o;
                        e.fail = i;
                        that.lbuploadimg1(e);
                    }
                }
            });
        },

        deleteFun: function (e) {
            var a = e.currentTarget.dataset.index;
            var t = this.images;
            t.splice(a, 1);
            imgArray.splice(a, 1);
            console.log('删除images里的图片后剩余的图片', t, imgArray);
            this.setData({
                images: t
            });
        },

        delete1: function (e) {
            var a = e.currentTarget.dataset.index;
            var t = this.images1;
            t.splice(a, 1);
            console.log('删除images1里的图片后剩余的图片', t);
            this.setData({
                images1: t
            });
        },

        lbdelete: function (e) {
            var a = e.currentTarget.dataset.index;
            var t = this.lbimages;
            t.splice(a, 1);
            lbimgArray.splice(a, 1);
            console.log('删除lbimages里的图片后剩余的图片', lbimgArray);
            this.setData({
                lbimages: t
            });
        },

        lbdelete1: function (e) {
            var a = e.currentTarget.dataset.index;
            var t = this.lbimages1;
            t.splice(a, 1);
            lbimgArray1.splice(a, 1);
            console.log('删除lbimages1里的图片后剩余的图片', lbimgArray1);
            this.setData({
                lbimages1: t
            });
        },

        formSubmit: function (e) {
            var that = this;
            var t = that.images1;
            console.log(
                'imgArray',
                imgArray,
                'imgArray1',
                imgArray1,
                'images',
                that.images,
                'images1',
                that.images1,
                'lbimgArray',
                lbimgArray,
                'lbimgArray1',
                lbimgArray1,
                'lbimages',
                that.lbimages,
                'lbimages1',
                that.lbimages1
            );
            var o = uni.getStorageSync('sjdsjid');
            console.log(o);
            console.log('form发生了submit事件，携带数据为：', e.detail.value);
            console.log(imglogo, imgArray, imgArray1);
            var i = this.yzm;
            console.log('随机生成的验证码', i);
            var l = e.detail.value.sjmc;
            var s = e.detail.value.sjdh;
            var n = e.detail.value.mdwz;
            var c = this.jwd;
            var r = e.detail.value.rjj;
            var g = e.detail.value.qsj;
            var u = e.detail.value.xyh;
            var d = e.detail.value.xyhje;
            var m = e.detail.value.mdgg;
            console.log(l, s, n, c, r, g, u, d, m);
            var h = '';
            var f = true;
            if ('' == imglogo) h = '请上传商家Logo！';
            else if ('' == l) h = '请填写商家名称！';
            else if ('' == s) h = '请填写商家手机号！';
            else if (11 != s.length) h = '手机号错误！';
            else if ('' == n) h = '请填写门店位置';
            else if ('' == c) h = '请点击定位按钮进行定位';
            else if ('' == r) h = '请填写人均价';
            else if ('' == g) h = '请填写起送价';
            else if ('' == d) h = '请填写新用户优惠金额';
            else if ('' == m) {
                h = '请填写门店公告';
            } else {
                var p = function () {
                    var e = imgArray.concat(imgArray1);
                    console.log('请求接口', e, e.toString());
                    app.globalData.util.request({
                        url: 'entry/wxapp/UpdStoreInfo',
                        cachetime: '0',
                        data: {
                            id: o,
                            logo: imglogo,
                            name: l,
                            tel: s,
                            address: n,
                            coordinates: c,
                            capita: r,
                            start_at: g,
                            announcement: m,
                            xyh_money: d,
                            xyh_open: u ? 1 : 2,
                            environment: e.toString()
                        },
                        success: function (e) {
                            if ('1' == e.data) {
                                uni.showModal({
                                    title: '提示',
                                    content: '提交成功'
                                });
                                setTimeout(function () {
                                    uni.navigateBack({});
                                }, 1000);
                            } else {
                                if ('2' == e.data) {
                                    uni.showModal({
                                        title: '提示',
                                        content: '请修改后提交'
                                    });
                                } else {
                                    uni.showToast({
                                        title: '网络错误'
                                    });
                                }
                            }
                            console.log('Assess', e.data);
                        }
                    });
                };
                f = false;
                uni.showLoading({
                    title: '正在提交',
                    mask: true
                });
                if (0 == t.length) {
                    p();
                } else {
                    (function e(a) {
                        var t = a.i ? a.i : 0;
                        var o = a.success ? a.success : 0;
                        var i = a.fail ? a.fail : 0;
                        uni.uploadFile({
                            url: a.url,
                            filePath: a.path[t],
                            name: 'upfile',
                            formData: null,
                            success: function (e) {
                                if ('' != e.data) {
                                    console.log(e);
                                    o++;
                                    imgArray1.push(e.data);
                                    console.log(t);
                                    console.log('图片数组', imgArray1);
                                } else {
                                    uni.showToast({
                                        icon: 'loading',
                                        title: '请重试'
                                    });
                                }
                            },
                            fail: function (e) {
                                i++;
                                console.log('fail:' + t + 'fail:' + i);
                            },
                            complete: function () {
                                console.log(t);
                                if (++t == a.path.length) {
                                    uni.hideToast();
                                    console.log('执行完毕');
                                    p();
                                    console.log('成功：' + o + ' 失败：' + i);
                                } else {
                                    console.log(t);
                                    a.i = t;
                                    a.success = o;
                                    a.fail = i;
                                    e(a);
                                }
                            }
                        });
                    })({
                        url: siteinfo.siteroot + '?i=' + siteinfo.uniacid + '&c=entry&a=wxapp&do=upload&m=ymq_canyino2o',
                        path: t
                    });
                }
            }
            if (1 == f) {
                uni.showModal({
                    title: '提示',
                    content: h
                });
            }
        },

        cxkt: function () {
            this.setData({
                issq: true
            });
        },

        gongg: function (e) {
            console.log(e.detail.value);
            var a = parseInt(e.detail.value.length);
            this.setData({
                zsnum: a
            });
        },

        getPhoneNumber: function (e) {
            var that = this;
            console.log(e);
            console.log(e.detail.iv);
            console.log(e.detail.encryptedData);
            if ('getPhoneNumber:fail user deny' == e.detail.errMsg) {
                uni.showModal({
                    title: '提示',
                    showCancel: false,
                    content: '您未授权获取您的手机号',
                    success: function (e) {}
                });
            } else {
                app.globalData.util.request({
                    url: 'entry/wxapp/Jiemi',
                    cachetime: '0',
                    data: {
                        sessionKey: getApp().globalData.getSK,
                        data: e.detail.encryptedData,
                        iv: e.detail.iv
                    },
                    success: function (e) {
                        console.log('解密后的数据', e);
                        if (null != e.data.phoneNumber) {
                            that.setData({
                                sjh: e.data.phoneNumber,
                                isbd: true,
                                bdsjhtext: '验证成功'
                            });
                        }
                    }
                });
            }
        },

        formReset() {
            console.log('占位：函数 formReset 未声明');
        }
    }
};
</script>
<style>
@import './sjzl.css';
</style>
